<?php 

namespace app\admin\event;

use app\common\model\AdvPosition as AdvPositionModel;
use app\common\utils\JsonService;

class AdvPositionEvent{

    /*
    * 广告位列表
    */
    public static function advPositionList(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where = [];
        if($id = isset($params['id'])){
                    $where[] = [['id','=',$params['id']]];
            }
            if($ap_name = isset($params['ap_name'])){
                    $where[] = [['ap_name','like',"%{$params['ap_name']}%"]];
            }
            if($is_use = isset($params['is_use'])){
                    $where[] = [['is_use','like',"%{$params['is_use']}%"]];
            }
            if($ap_height = isset($params['ap_height'])){
                    $where[] = [['ap_height','like',"%{$params['ap_height']}%"]];
            }
            if($ap_width = isset($params['ap_width'])){
                    $where[] = [['ap_width','like',"%{$params['ap_width']}%"]];
            }
            if($shop_id = isset($params['shop_id'])){
                    $where[] = [['shop_id','like',"%{$params['shop_id']}%"]];
            }
             $condition = [
            'where' => $where,
            'order' => ['id' => 'desc'],
                'paginate' => [
                'page' => $page,
                'query' => $params,
            ],
        ];
        $advPosition = AdvPositionModel::getPageAll($condition);
        return $advPosition;
    }


    /*
    * 新增广告位
    */
    public static function createAdvPosition($data){

        try {
            AdvPositionModel::create($data);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
    * 更新广告位
    */
    public static function updateAdvPosition($data){

        try {
            AdvPositionModel::updateOne($data, ['id' => $data['id']]);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }


}

